package com.itheima.d3_annotation;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/*
    目标：掌握注解的解析。

    AnnotatedElement接口提供了解析注解的方法
        public Annotation[] getDeclaredAnnotations()	获取当前对象上面的注解。
        public T getDeclaredAnnotation(Class<T> annotationClass)	获取指定的注解对象
        public boolean isAnnotationPresent(Class<Annotation> annotationClass)	判断当前对象上是否存在某个注解
 */
public class AnnotationTest3 {

    @Test
    public void parseClass() {
        Class booksClass = Books.class;
        boolean annotationPresent = booksClass.isAnnotationPresent(Book.class);
        if (annotationPresent) {
            Book declaredAnnotation = (Book) booksClass.getDeclaredAnnotation(Book.class);
            System.out.println(declaredAnnotation.name());
            System.out.println(declaredAnnotation.price());
        }
    }

    @Test
    public void parseMethod() throws Exception {
        Class booksClass = Books.class;
        Method declaredMethods = booksClass.getDeclaredMethod("show");
        boolean annotationPresent = declaredMethods.isAnnotationPresent(Book.class);
        if (annotationPresent) {
            Book book = declaredMethods.getDeclaredAnnotation(Book.class);
            System.out.println(book.name());
            System.out.println(book.price());
        }
    }
}
